//
//  GasTableViewCell.m
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "GasTableViewCell.h"

#define LEFT_COLUMN_OFFSET 10
#define LEFT_COLUMN_WIDTH 130

#define MIDDLE_COLUMN_OFFSET 140
#define MIDDLE_COLUMN_WIDTH 110

#define RIGHT_COLUMN_OFFSET 270

#define UPPER_ROW_TOP 8
#define Middle_Row_Top 34
#define LOWER_ROW_TOP 50

#define MAIN_FONT_SIZE 18
#define MIN_MAIN_FONT_SIZE 16
#define SECONDARY_FONT_SIZE 12
#define MIN_SECONDARY_FONT_SIZE 10
#pragma mark -

@implementation GasTableViewCell


static UIFont * mainFont		= nil;
static UIFont * secondaryFont	= nil; 

+ (void)initialize
{
	if(self == [GasTableViewCell class])
	{
		mainFont = [[UIFont systemFontOfSize:18] retain];
		secondaryFont  = [[UIFont systemFontOfSize:12] retain];
		
		// this is a good spot to load any graphics you might be drawing in -drawContentView:
		// just load them and retain them here (ONLY if they're small enough that you don't care about them wasting memory)
		// the idea is to do as LITTLE work (e.g. allocations) in -drawContentView: as possible
	}
}

 
	
- (void)dealloc
{
	[topLeftText release];
	[topRightText release];
	[middleLeftText release];
	[middleRightText release];
	[lowerLeftText release];
	[lowerRightText release];
    [super dealloc];
}


#pragma mark -
 
- (NSString *)topLeftText {
    return [[topLeftText retain] autorelease];
}

- (void)setTopLeftText:(NSString *)value {
    if (topLeftText != value) {
        [topLeftText release];
        topLeftText = [value copy];
    }
}

- (NSString *)topRightText {
    return [[topRightText retain] autorelease];
}

- (void)setTopRightText:(NSString *)value {
    if (topRightText != value) {
        [topRightText release];
        topRightText = [value copy];
    }
}

- (NSString *)middleLeftText {
    return [[middleLeftText retain] autorelease];
}

- (void)setMiddleLeftText:(NSString *)value {
    if (middleLeftText != value) {
        [middleLeftText release];
        middleLeftText = [value copy];
    }
}

- (NSString *)middleRightText {
    return [[middleRightText retain] autorelease];
}

- (void)setMiddleRightText:(NSString *)value {
    if (middleRightText != value) {
        [middleRightText release];
        middleRightText = [value copy];
    }
}

- (NSString *)lowerLeftText {
    return [[lowerLeftText retain] autorelease];
}

- (void)setLowerLeftText:(NSString *)value {
    if (lowerLeftText != value) {
        [lowerLeftText release];
        lowerLeftText = [value copy];
    }
}

- (NSString *)lowerRightText {
    return [[lowerRightText retain] autorelease];
}

- (void)setLowerRightText:(NSString *)value {
    if (lowerRightText != value) {
        [lowerRightText release];
        lowerRightText = [value copy];
    }
}



#pragma mark -
- (void)drawContentView:(CGRect)retangle
{
	CGContextRef context = UIGraphicsGetCurrentContext();

	UIColor * backgroundColor = [UIColor lightGrayColor];
//	UIColor *textColor = [UIColor blackColor];

	// Color and font for the main text items (Date and Cost)
	UIColor *mainTextColor = nil;
	// Color and font for the secondary text items (Odometer and Volume)
	UIColor *secondaryTextColor = nil;

/*	if(self.selected)
	{
		backgroundColor = [UIColor lightGrayColor];
		mainTextColor = [UIColor whiteColor];
		secondaryTextColor = [UIColor whiteColor];
	}
	if (self.highlighted) {
		mainTextColor = [UIColor whiteColor];
		secondaryTextColor = [UIColor whiteColor];
	}
	else {*/
		mainTextColor = [UIColor blackColor];
		secondaryTextColor = [UIColor darkTextColor];//  GrayColor];
		backgroundColor = [UIColor  lightGrayColor]; //whiteColor];
//	}

	
	
//*
	[backgroundColor set];
	CGContextFillRect(context, retangle);

	CGRect contentRect = self.bounds;
	if (!self.editing) {
	CGFloat boundsX = contentRect.origin.x;
	CGPoint point;
	
	CGFloat actualFontSize;
	CGSize size;
	
	// Set the color for the main text items
	[mainTextColor set];
	
	/*
	 Draw the date top left; use the NSString UIKit method to scale the font size down if the text does not fit in the given area
	 */
	point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP);
	[topLeftText  drawAtPoint:point 
												  forWidth:LEFT_COLUMN_WIDTH 
												  withFont:mainFont 
											   minFontSize:MIN_MAIN_FONT_SIZE 
											actualFontSize:NULL 
											 lineBreakMode:UILineBreakModeTailTruncation 
										baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	
	/*
	 Draw the cost, right-aligned in the middle column.
	 To ensure it is right-aligned, first find its width with the given font and minimum allowed font size. Then draw the string at the appropriate offset.
	 */
	size = [topRightText sizeWithFont:mainFont 
						minFontSize:MIN_MAIN_FONT_SIZE 
					 actualFontSize:&actualFontSize 
						   forWidth:MIDDLE_COLUMN_WIDTH 
					  lineBreakMode:UILineBreakModeTailTruncation];
	point = CGPointMake(boundsX + MIDDLE_COLUMN_OFFSET + MIDDLE_COLUMN_WIDTH - size.width, UPPER_ROW_TOP);
	[topRightText drawAtPoint:point 
				   forWidth:MIDDLE_COLUMN_WIDTH 
				   withFont:mainFont 
				minFontSize:actualFontSize 
			 actualFontSize:&actualFontSize 
			  lineBreakMode:UILineBreakModeTailTruncation 
		 baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	
	// Set the color for the secondary text items
	[secondaryTextColor set];
	
	/*
	 Draw the odometer left column, use the NSString UIKit method to scale the font size down if the text does not fit in the given area
	 */
	point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, Middle_Row_Top);
	[middleLeftText drawAtPoint:point 
					   forWidth:LEFT_COLUMN_WIDTH 
					   withFont:secondaryFont 
					minFontSize:MIN_SECONDARY_FONT_SIZE 
				 actualFontSize:NULL 
				  lineBreakMode:UILineBreakModeTailTruncation 
			 baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	
	/*
	 Draw the fuel string, right-aligned in the middle column.
	 To ensure it is right-aligned, first find its width with the given font and minimum allowed font size. Then draw the string at the appropriate offset.
	 */
	size = [middleRightText sizeWithFont:secondaryFont 
						  minFontSize:MIN_SECONDARY_FONT_SIZE 
					   actualFontSize:&actualFontSize 
							 forWidth:MIDDLE_COLUMN_WIDTH 
						lineBreakMode:UILineBreakModeTailTruncation];
	point = CGPointMake(boundsX + MIDDLE_COLUMN_OFFSET + MIDDLE_COLUMN_WIDTH - size.width, Middle_Row_Top);
	[middleRightText drawAtPoint:point 
					 forWidth:LEFT_COLUMN_WIDTH 
					 withFont:secondaryFont 
				  minFontSize:actualFontSize 
			   actualFontSize:&actualFontSize 
				lineBreakMode:UILineBreakModeTailTruncation 
		   baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	
#pragma mark here alloc consumptionString and tripDistanceString
  point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, LOWER_ROW_TOP);
	 [lowerLeftText drawAtPoint:point 
	 forWidth:LEFT_COLUMN_WIDTH 
	 withFont:secondaryFont 
	 minFontSize:MIN_SECONDARY_FONT_SIZE 
	 actualFontSize:NULL 
	 lineBreakMode:UILineBreakModeTailTruncation 
	 baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	 
	 
	 size = [lowerRightText sizeWithFont:secondaryFont 
	 minFontSize:MIN_SECONDARY_FONT_SIZE 
	 actualFontSize:&actualFontSize 
	 forWidth:MIDDLE_COLUMN_WIDTH 
	 lineBreakMode:UILineBreakModeTailTruncation];
	 point = CGPointMake(boundsX + MIDDLE_COLUMN_OFFSET + MIDDLE_COLUMN_WIDTH - size.width, LOWER_ROW_TOP);
	 [ lowerRightText drawAtPoint:point 
	 forWidth:LEFT_COLUMN_WIDTH 
	 withFont:secondaryFont 
	 minFontSize:actualFontSize 
	 actualFontSize:&actualFontSize 
	 lineBreakMode:UILineBreakModeTailTruncation 
	 baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	 // Drawing code

	} // end if editing
}
@end
